package fichier;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


@SuppressWarnings("serial")
public class IHMSortie extends JFrame implements ActionListener,KeyListener{
	private JPanel pan;
	private JLabel libDate;
	private JLabel libDest;
	private JLabel libNomExt;
	private JLabel libNum;
	private JTextField dateSortie;
	private JComboBox destination;
	private JTextField nomExt;
	private JTextField numSortie;
	private JButton valider;
	private JButton annuler;
	
	private Modele modele;
	
	private String[] tab = {"Externe","Usine"};
	
	public IHMSortie(Modele modele){
		super("Sortie");
		this.setBounds(100, 100, 400, 400);
		
		this.modele = modele;
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.pan = new JPanel();
		this.libDate = new JLabel("Date");
		this.libDest = new JLabel("Destination");
		this.libNomExt = new JLabel("Libellé");
		this.libNum = new JLabel("Numero");
		this.dateSortie = new JTextField("JJ-MM-AAAA");
		this.destination = new JComboBox(tab);
		this.nomExt = new JTextField("");
		this.numSortie = new JTextField("");
		this.valider = new JButton("Valider");
		this.annuler = new JButton("Annuler");
		this.pan.setBorder(BorderFactory.createTitledBorder("Sortie"));
		
		this.destination.addActionListener(this);
		this.valider.addActionListener(this);
		this.annuler.addActionListener(this);
		
		this.valider.addKeyListener(this);
		this.annuler.addKeyListener(this);
		
		this.getContentPane().add(this.pan);
		this.pan.add(libDate);
		this.pan.add(libDest);
		this.pan.add(libNomExt);
		this.pan.add(libNum);
		this.pan.add(dateSortie);
		this.pan.add(destination);
		this.pan.add(nomExt);
		this.pan.add(numSortie);
		this.pan.add(valider);
		this.pan.add(annuler);
		
		this.getContentPane().setLayout(null);
		this.pan.setLayout(null);
		
		this.pan.setBounds(25, 20, 350, 310);
		this.libDate.setBounds(50, 50, 100, 25);
		this.libDest.setBounds(50, 100, 100, 25);
		this.libNomExt.setBounds(50, 150, 100, 25);
		this.libNum.setBounds(50, 200, 100, 25);
		this.dateSortie.setBounds(170, 50, 150, 25);
		this.destination.setBounds(170, 100, 150, 25);
		this.nomExt.setBounds(170, 150, 150, 25);
		this.numSortie.setBounds(170, 200, 150, 25);
		this.valider.setBounds(100, 250, 100, 25);
		this.annuler.setBounds(220, 250, 100, 25);
		
		this.nomExt.setEditable(true);
		
		this.setResizable(false);
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getSource() == this.valider){
			//new sortie(modele modele,lieu destination,string numerobon,string date);
			String error = new String("");
			int anneeAddTest=1;
			String anneeAdd			= dateSortie.getText();
			if(!Pattern.compile("(0[1-9]|[1-2][0-9]|30|31)-(0[1-9]|1[0-2])-([0-9]{4})").matcher(anneeAdd).matches()){
				error = error + "La date est incorrecte\n";
				anneeAddTest = 0;
			}
			else{
				SelectData.REQUETE = "SELECT YEAR( SYSDATE() ) AS annee, DAYOFMONTH( SYSDATE() ) AS jour, MONTH( SYSDATE() ) AS mois FROM DUAL";
				ResultSet res = SelectData.startSelect();
				try{
					if(res.next() && anneeAddTest==1){
						if( (Integer.parseInt(anneeAdd.substring(6)) > Integer.parseInt(res.getString("annee")))
								|| ((Integer.parseInt(anneeAdd.substring(6)) == Integer.parseInt(res.getString("annee"))) && (Integer.parseInt(anneeAdd.substring(3,5)) > Integer.parseInt(res.getString("mois"))))
								|| ((Integer.parseInt(anneeAdd.substring(6)) == Integer.parseInt(res.getString("annee"))) && (Integer.parseInt(anneeAdd.substring(3,5)) == Integer.parseInt(res.getString("mois"))) && (Integer.parseInt(anneeAdd.substring(0,2)) > Integer.parseInt(res.getString("jour"))))){
							error = error + "La date est incorrecte\n";
						}	
				}
				}catch(SQLException ex){
					System.out.println("IL Y A UN PROBLEME");
				}
			}
			if(this.destination.getSelectedItem().equals("Externe") && this.nomExt.getText().equals("")){
				error = error + "Libellé non renseigné\n";
			}
			if(this.numSortie.getText().equals("")){
				error = error + "Numero non renseigné\n";
			}
			
			if(!error.equals("")){
				JOptionPane.showMessageDialog(this,error, "Attention", JOptionPane.OK_OPTION,new ImageIcon(getClass().getResource("/icon/warn.png")));
			}
			else{
				if(this.destination.getSelectedItem().equals("Externe")){
					@SuppressWarnings("unused")
					Sortie s = new Sortie(this.modele, new LieuExterne(this.nomExt.getText()), this.numSortie.getText(), this.dateSortie.getText());
				}
				else{
					@SuppressWarnings("unused")
					Sortie s = new Sortie(this.modele, new LieuUsine(), this.numSortie.getText(), this.dateSortie.getText());
				}
				if(main.testUpdate)
					JOptionPane.showMessageDialog(this,"La sortie a bien été ajoutée", "Model Manager", JOptionPane.OK_OPTION,new ImageIcon(getClass().getResource("/icon/ok.png")));
				else
					JOptionPane.showMessageDialog(this,"La sortie n'a pas pu être ajoutée", "Attention", JOptionPane.OK_OPTION,new ImageIcon(getClass().getResource("/icon/warn.png")));
			
				this.dispose();
				@SuppressWarnings("unused")
				AfficherModele a = new AfficherModele(this.modele.getID());
			}
		}
		else if(arg0.getSource() == this.annuler){
			this.dispose();
			@SuppressWarnings("unused")
			AfficherModele a = new AfficherModele(modele.getID());
		}
		else if(arg0.getSource() == this.destination){
			if(this.destination.getSelectedItem() == "Externe"){
				this.nomExt.setEditable(true);
			}
			else{
				this.nomExt.setEditable(false);
				this.nomExt.setText(null);
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
